/* @(#)NodeFragGovernanceSerialization.java
** 
** Copyright (C) 2012 by Steven D. Stamps
**
**             Trademarks & Copyrights
** Flywheel Management Science(TM), Flywheel Management Model(TM),
** Flywheel Story Editor(TM) and FlywheelMS(TM) are exclusive trademarks
** of Steven D. Stamps and may only be used freely for the purpose of
** identifying the unforked version of this software.  Subsequent forks
** may not use these trademarks.  All other rights are reserved.
**
** DecKanGL (Decorated Kanban Glyph Language) and TribKn (Tribal Knowledge)
** are also exclusive trademarks of Steven D. Stamps.  These may be used
** freely within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** gConGUI (game Controller Graphical User Interface) is an exclusive
** trademark of Steven D. Stamps.  This trademark may be used freely
** within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** Trademark information is available at
** <http://www.flywheelms.com/trademarks>
**
** Flywheel Management Science(TM) is a copyrighted body of management
** metaphors, governance processes, and leadership techniques that is
** owned by Steven D. Stamps.  These copyrighted materials may be freely
** used, without alteration, by the community (users and developers)
** surrounding this GPL3-licensed software.  Additional copyright
** information is available at <http://www.flywheelms.org/copyrights>
**
**              GPL3 Software License
** This program is free software: you can use it, redistribute it and/or
** modify it under the terms of the GNU General Public License, version 3,
** as published by the Free Software Foundation. This program is distributed
** in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
** even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
** PURPOSE.  See the GNU General Public License for more details. You should
** have received a copy of the GNU General Public License, in a file named
** COPYING, along with this program.  If you cannot find your copy, see
** <http://www.gnu.org/licenses/gpl-3.0.html>.
*/

package com.flywheelms.library.fmm.serialization;

import org.json.JSONException;
import org.json.JSONObject;

import com.flywheelms.library.fca.FlywheelCommunityAuthentication;
import com.flywheelms.library.fmm.helper.FmmDateHelper;
import com.flywheelms.library.fmm.node.impl.enumerator.AssignmentCommitmentType;
import com.flywheelms.library.fmm.node.impl.enumerator.GovernanceRole;
import com.flywheelms.library.fmm.node.impl.headline.CommunityMember;
import com.flywheelms.library.fmm.node.impl.nodefrag.NodeFragGovernance;
import com.flywheelms.library.util.JsonHelper;

public abstract class NodeFragGovernanceSerialization {
	
	public static final String key__NODE_FRAG__GOVERNANCE = "NodeFragGovernance";

	public static final String key__GOVERNANCE__NODE_ID = "NodeId";
	public static final String key__GOVERNANCE__PARENT_NODE_ID = "ParentNodeId";

	public static final String key__GOVERNANCE__SPONSOR__NODE_ID = "SponsorNodeId";
	public static final String key__GOVERNANCE__SPONSOR__ASSIGNMENT_COMMITMENT_TYPE = "SponsorAssignmentCommitmentType";
	public static final String key__GOVERNANCE__SPONSOR__COMMITMENT_TIMESTAMP = "SponsorCommitmentTimestamp";
	public static final String key__GOVERNANCE__SPONSOR__PROPOSED_BY_NODE_ID = "SponsorProposedByNodeId";
	public static final String key__GOVERNANCE__SPONSOR__PROPOSED_TIMESTAMP = "SponsorProposedTimestamp";
	public static final String key__GOVERNANCE__SPONSOR__LOCKED_BY_NODE_ID = "SponsorLockedByNodeId";
	public static final String key__GOVERNANCE__SPONSOR__LOCKED_TIMESTAMP = "SponsorLockedTimestamp";

	public static final String key__GOVERNANCE__CUSTOMER__NODE_ID = "CustomerNodeId";
	public static final String key__GOVERNANCE__CUSTOMER__ASSIGNMENT_COMMITMENT_TYPE = "CustomerAssignmentCommitmentType";
	public static final String key__GOVERNANCE__CUSTOMER__COMMITMENT_TIMESTAMP = "CustomerCommitmentTimestamp";
	public static final String key__GOVERNANCE__CUSTOMER__PROPOSED_BY_NODE_ID = "CustomerProposedByNodeId";
	public static final String key__GOVERNANCE__CUSTOMER__PROPOSED_TIMESTAMP = "CustomerProposedTimestamp";
	public static final String key__GOVERNANCE__CUSTOMER__LOCKED_BY_NODE_ID = "CustomerLockedByNodeId";
	public static final String key__GOVERNANCE__CUSTOMER__LOCKED_TIMESTAMP = "CustomerLockedTimestamp";

	public static final String key__GOVERNANCE__FACILITATOR__NODE_ID = "FacilitatorNodeId";
	public static final String key__GOVERNANCE__FACILITATOR__ASSIGNMENT_COMMITMENT_TYPE = "FacilitatorAssignmentCommitmentType";
	public static final String key__GOVERNANCE__FACILITATOR__COMMITMENT_TIMESTAMP = "FacilitatorCommitmentTimestamp";
	public static final String key__GOVERNANCE__FACILITATOR__PROPOSED_BY_NODE_ID = "FacilitatorProposedByNodeId";
	public static final String key__GOVERNANCE__FACILITATOR__PROPOSED_TIMESTAMP = "FacilitatorProposedTimestamp";
	public static final String key__GOVERNANCE__FACILITATOR__LOCKED_BY_NODE_ID = "FacilitatorLockedByNodeId";
	public static final String key__GOVERNANCE__FACILITATOR__LOCKED_TIMESTAMP = "FacilitatorLockedTimestamp";

	public static final String key__GOVERNANCE__ADMINISTRATOR__NODE_ID = "AdministratorNodeId";
	public static final String key__GOVERNANCE__ADMINISTRATOR__ASSIGNMENT_COMMITMENT_TYPE = "AdministratorAssignmentCommitmentType";
	public static final String key__GOVERNANCE__ADMINISTRATOR__COMMITMENT_TIMESTAMP = "AdministratorCommitmentTimestamp";
	public static final String key__GOVERNANCE__ADMINISTRATOR__PROPOSED_BY_NODE_ID = "AdministratorProposedByNodeId";
	public static final String key__GOVERNANCE__ADMINISTRATOR__PROPOSED_TIMESTAMP = "AdministratorProposedTimestamp";
	public static final String key__GOVERNANCE__ADMINISTRATOR__LOCKED_BY_NODE_ID = "AdministratorLockedByNodeId";
	public static final String key__GOVERNANCE__ADMINISTRATOR__LOCKED_TIMESTAMP = "AdministratorLockedTimestamp";
	
	public static NodeFragGovernance getNodeFragGovernance(JSONObject aJsonObject) {
		try {
			NodeFragGovernance theGovernance = new NodeFragGovernance(
					aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__NODE_ID),
					aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__PARENT_NODE_ID) );
			getGovernanceAttributes(aJsonObject, theGovernance);
			return theGovernance;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	private static void getGovernanceAttributes(JSONObject aJsonObject, NodeFragGovernance aGovernance) {
		try {
			aGovernance.rehydrateGovernanceTeamMember(
					GovernanceRole.SPONSOR,
					FlywheelCommunityAuthentication.getInstance().getCommunityMember(aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__SPONSOR__NODE_ID)),
					AssignmentCommitmentType.getObjectForName(aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__SPONSOR__ASSIGNMENT_COMMITMENT_TYPE)),
					JsonHelper.getDate(aJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__SPONSOR__COMMITMENT_TIMESTAMP),
					FlywheelCommunityAuthentication.getInstance().getCommunityMember(aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__SPONSOR__PROPOSED_BY_NODE_ID)),
					JsonHelper.getDate(aJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__SPONSOR__PROPOSED_TIMESTAMP),
					FlywheelCommunityAuthentication.getInstance().getCommunityMember(aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__SPONSOR__LOCKED_BY_NODE_ID)),
					JsonHelper.getDate(aJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__CUSTOMER__LOCKED_TIMESTAMP) );

			aGovernance.rehydrateGovernanceTeamMember(
					GovernanceRole.CUSTOMER,
					FlywheelCommunityAuthentication.getInstance().getCommunityMember(aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__CUSTOMER__NODE_ID)),
					AssignmentCommitmentType.getObjectForName(aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__CUSTOMER__ASSIGNMENT_COMMITMENT_TYPE)),
					JsonHelper.getDate(aJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__CUSTOMER__COMMITMENT_TIMESTAMP),
					FlywheelCommunityAuthentication.getInstance().getCommunityMember(aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__CUSTOMER__PROPOSED_BY_NODE_ID)),
					JsonHelper.getDate(aJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__CUSTOMER__PROPOSED_TIMESTAMP),
					FlywheelCommunityAuthentication.getInstance().getCommunityMember(aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__CUSTOMER__LOCKED_BY_NODE_ID)),
					JsonHelper.getDate(aJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__CUSTOMER__LOCKED_TIMESTAMP) );

			aGovernance.rehydrateGovernanceTeamMember(
					GovernanceRole.FACILITATOR,
					FlywheelCommunityAuthentication.getInstance().getCommunityMember(aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__FACILITATOR__NODE_ID)),
					AssignmentCommitmentType.getObjectForName(aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__FACILITATOR__ASSIGNMENT_COMMITMENT_TYPE)),
					JsonHelper.getDate(aJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__FACILITATOR__COMMITMENT_TIMESTAMP),
					FlywheelCommunityAuthentication.getInstance().getCommunityMember(aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__FACILITATOR__PROPOSED_BY_NODE_ID)),
					JsonHelper.getDate(aJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__FACILITATOR__PROPOSED_TIMESTAMP),
					FlywheelCommunityAuthentication.getInstance().getCommunityMember(aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__FACILITATOR__LOCKED_BY_NODE_ID)),
					JsonHelper.getDate(aJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__FACILITATOR__LOCKED_TIMESTAMP) );

			aGovernance.rehydrateGovernanceTeamMember(
					GovernanceRole.ADMINISTRATOR,
					FlywheelCommunityAuthentication.getInstance().getCommunityMember(aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__ADMINISTRATOR__NODE_ID)),
					AssignmentCommitmentType.getObjectForName(aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__ADMINISTRATOR__ASSIGNMENT_COMMITMENT_TYPE)),
					JsonHelper.getDate(aJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__ADMINISTRATOR__COMMITMENT_TIMESTAMP),
					FlywheelCommunityAuthentication.getInstance().getCommunityMember(aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__ADMINISTRATOR__PROPOSED_BY_NODE_ID)),
					JsonHelper.getDate(aJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__ADMINISTRATOR__PROPOSED_TIMESTAMP),
					FlywheelCommunityAuthentication.getInstance().getCommunityMember(aJsonObject.getString(NodeFragGovernanceSerialization.key__GOVERNANCE__ADMINISTRATOR__LOCKED_BY_NODE_ID)),
					JsonHelper.getDate(aJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__ADMINISTRATOR__LOCKED_TIMESTAMP) );
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static JSONObject createNodeFragGovernanceObject(NodeFragGovernance aGovernance) {
		JSONObject theJsonObject = new JSONObject();
		try {
			theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__NODE_ID, aGovernance.getNodeId().getNodeIdString());
			theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__PARENT_NODE_ID, aGovernance.getParentNodeIdString());
			if(aGovernance.hasGovernanceRole(GovernanceRole.SPONSOR) && aGovernance.getTeamMember(GovernanceRole.SPONSOR) != null) {
				theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__SPONSOR__NODE_ID, aGovernance.getTeamMember(GovernanceRole.SPONSOR).getNodeIdString());
				theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__SPONSOR__ASSIGNMENT_COMMITMENT_TYPE, aGovernance.getCommitmentType(GovernanceRole.SPONSOR).toString());
				JsonHelper.putDate(theJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__SPONSOR__COMMITMENT_TIMESTAMP, aGovernance.getCommitmentTimestamp(GovernanceRole.SPONSOR));
				theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__SPONSOR__PROPOSED_BY_NODE_ID, aGovernance.getProposedBy(GovernanceRole.SPONSOR).getNodeIdString());
				JsonHelper.putDate(theJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__SPONSOR__PROPOSED_TIMESTAMP, aGovernance.getProposedTimestamp(GovernanceRole.SPONSOR));
				if(aGovernance.getLockedBy(GovernanceRole.SPONSOR) != null) {
					theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__SPONSOR__LOCKED_BY_NODE_ID, aGovernance.getLockedBy(GovernanceRole.SPONSOR).getNodeIdString());
					JsonHelper.putDate(theJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__SPONSOR__LOCKED_TIMESTAMP, aGovernance.getLockedTimestamp(GovernanceRole.SPONSOR));
				} else {
					theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__SPONSOR__LOCKED_BY_NODE_ID, CommunityMember.getNullValue().getNodeIdString());
					JsonHelper.putDate(theJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__SPONSOR__LOCKED_TIMESTAMP, FmmDateHelper.getNullDate());
				}
			}
			if(aGovernance.hasGovernanceRole(GovernanceRole.CUSTOMER) && aGovernance.getTeamMember(GovernanceRole.CUSTOMER) != null) {
				theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__CUSTOMER__NODE_ID, aGovernance.getTeamMember(GovernanceRole.CUSTOMER).getNodeIdString());
				theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__CUSTOMER__ASSIGNMENT_COMMITMENT_TYPE, aGovernance.getCommitmentType(GovernanceRole.CUSTOMER).toString());
				JsonHelper.putDate(theJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__CUSTOMER__COMMITMENT_TIMESTAMP, aGovernance.getCommitmentTimestamp(GovernanceRole.CUSTOMER));
				theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__CUSTOMER__PROPOSED_BY_NODE_ID, aGovernance.getProposedBy(GovernanceRole.CUSTOMER).getNodeIdString());
				JsonHelper.putDate(theJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__CUSTOMER__PROPOSED_TIMESTAMP, aGovernance.getProposedTimestamp(GovernanceRole.CUSTOMER));
				if(aGovernance.getLockedBy(GovernanceRole.CUSTOMER) != null) {
					theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__CUSTOMER__LOCKED_BY_NODE_ID, aGovernance.getLockedBy(GovernanceRole.CUSTOMER).getNodeIdString());
					JsonHelper.putDate(theJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__CUSTOMER__LOCKED_TIMESTAMP, aGovernance.getLockedTimestamp(GovernanceRole.CUSTOMER));
				} else { 
					theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__CUSTOMER__LOCKED_BY_NODE_ID, CommunityMember.getNullValue().getNodeIdString());
					JsonHelper.putDate(theJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__CUSTOMER__LOCKED_TIMESTAMP, FmmDateHelper.getNullDate());
				}
			}
			if(aGovernance.hasGovernanceRole(GovernanceRole.FACILITATOR) && aGovernance.getTeamMember(GovernanceRole.FACILITATOR) != null) {
				theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__FACILITATOR__NODE_ID, aGovernance.getTeamMember(GovernanceRole.FACILITATOR).getNodeIdString());
				theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__FACILITATOR__ASSIGNMENT_COMMITMENT_TYPE, aGovernance.getCommitmentType(GovernanceRole.FACILITATOR).toString());
				JsonHelper.putDate(theJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__FACILITATOR__COMMITMENT_TIMESTAMP, aGovernance.getCommitmentTimestamp(GovernanceRole.FACILITATOR));
				theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__FACILITATOR__PROPOSED_BY_NODE_ID, aGovernance.getProposedBy(GovernanceRole.FACILITATOR).getNodeIdString());
				JsonHelper.putDate(theJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__FACILITATOR__PROPOSED_TIMESTAMP, aGovernance.getProposedTimestamp(GovernanceRole.FACILITATOR));
				if(aGovernance.getLockedBy(GovernanceRole.FACILITATOR) != null) {
					theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__FACILITATOR__LOCKED_BY_NODE_ID, aGovernance.getLockedBy(GovernanceRole.FACILITATOR).getNodeIdString());
					JsonHelper.putDate(theJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__FACILITATOR__LOCKED_TIMESTAMP, aGovernance.getLockedTimestamp(GovernanceRole.FACILITATOR));
				} else {
					theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__FACILITATOR__LOCKED_BY_NODE_ID, CommunityMember.getNullValue().getNodeIdString());
					JsonHelper.putDate(theJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__FACILITATOR__LOCKED_TIMESTAMP, FmmDateHelper.getNullDate());
				}
			}
			if(aGovernance.hasGovernanceRole(GovernanceRole.ADMINISTRATOR) && aGovernance.getTeamMember(GovernanceRole.ADMINISTRATOR) != null) {
				theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__ADMINISTRATOR__NODE_ID, aGovernance.getTeamMember(GovernanceRole.ADMINISTRATOR).getNodeIdString());
				theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__ADMINISTRATOR__ASSIGNMENT_COMMITMENT_TYPE, aGovernance.getCommitmentType(GovernanceRole.ADMINISTRATOR).toString());
				JsonHelper.putDate(theJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__ADMINISTRATOR__COMMITMENT_TIMESTAMP, aGovernance.getCommitmentTimestamp(GovernanceRole.ADMINISTRATOR));
				theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__ADMINISTRATOR__PROPOSED_BY_NODE_ID, aGovernance.getProposedBy(GovernanceRole.ADMINISTRATOR).getNodeIdString());
				JsonHelper.putDate(theJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__ADMINISTRATOR__PROPOSED_TIMESTAMP, aGovernance.getProposedTimestamp(GovernanceRole.ADMINISTRATOR));
				if(aGovernance.getLockedBy(GovernanceRole.ADMINISTRATOR) != null) {
					theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__ADMINISTRATOR__LOCKED_BY_NODE_ID, aGovernance.getLockedBy(GovernanceRole.ADMINISTRATOR).getNodeIdString());
					JsonHelper.putDate(theJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__ADMINISTRATOR__LOCKED_TIMESTAMP, aGovernance.getLockedTimestamp(GovernanceRole.ADMINISTRATOR));
				} else {
					theJsonObject.put(NodeFragGovernanceSerialization.key__GOVERNANCE__ADMINISTRATOR__LOCKED_BY_NODE_ID, CommunityMember.getNullValue().getNodeIdString());
					JsonHelper.putDate(theJsonObject, NodeFragGovernanceSerialization.key__GOVERNANCE__ADMINISTRATOR__LOCKED_TIMESTAMP, FmmDateHelper.getNullDate());
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return theJsonObject;
	}

}
